package com.nannybear.Controller;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nannybear.Model.Event;
import com.nannybear.R;

import java.util.ArrayList;
import java.util.List;

/**
 * \brief Controller of the calendar view
 * \author Audrey LOISEAU
 * \date 11-05-2015
 */


/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class GridCellAdapter  extends BaseAdapter {
    /********** DECLARATIONS **********/
    //Variables
    private List<String> list;
    private List<RowItem> rowItems;
    private String dateOfEvent;
    private int positionToDelete;
    private static final String LEISURE = "leisure";
    private static final String HEALTH = "health";
    private static final String OTHER = "other";

    //Class
    private CalendarController calendarController;

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param calendarController    The calendarController class
     * \param list  Each day in the month with the good color
     */
    public GridCellAdapter(CalendarController calendarController, ArrayList list){
        this.list = list;
        rowItems = new ArrayList<>();
        this.calendarController = calendarController;
        dateOfEvent = "";
    }

    /**
     * \brief Update the view with the good text and color
     * \param day_color The date with the good color
     */
    public void updateView(String[] day_color){
        final String theday = day_color[0];
        final String themonth = day_color[2];
        final String theyear = day_color[3];

        calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getDay().setText(theday);
        if(calendarController.getEventsCalendar().getEvent(theday+""+themonth+""+theyear) != null) {
            // Set the icon visible if an event exist on the day
            Event[] e = calendarController.getEventsCalendar().getEvent(theday + "" + themonth + "" + theyear);
            for (int i=0; i<e.length;i++){
                if(e[i].icon.equals(HEALTH)){
                    calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getEvent_health().setVisibility(View.VISIBLE);
                }
                if(e[i].icon.equals(LEISURE)){
                    calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getEvent_leisure().setVisibility(View.VISIBLE);
                }
                if(e[i].icon.equals(OTHER)){
                    calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getEvent_other().setVisibility(View.VISIBLE);
                }
            }
        }

        if (day_color[1].equals("GREY")) {
            calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getDay().setTextColor(Color.LTGRAY);
        }

        if (day_color[1].equals("BLACK")) {
            calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getDay().setTextColor(Color.BLACK);
        }

        if (day_color[1].equals("TODAY")) {
            Log.e("GridcellA","today = "+calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getDay());
            calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getDay().setTextColor(Color.BLACK);
            calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getDay().setBackgroundResource(R.drawable.circle_day_drawable);
        }

        // Listener for a box of the calendar
        calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().getDayButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dateOfEvent = theday+""+themonth+""+theyear;
                rowItems.clear();
                if(calendarController.getEventsCalendar().getEvent(theday+""+themonth+""+theyear) != null) {
                    Event[] e = calendarController.getEventsCalendar().getEvent(theday + "" + themonth + "" + theyear);
                    addItem(e);
                }
                calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().displayPopUp(dateOfEvent);
            }
        });
    }

    /**
     * \brief Add an item in the list view
     * \param event The new event to add
     */
    public void addItem(Event[] event){
        ListView listView;
        int drawable = 0;
        for(int i=0;i<event.length;i++) {
            if (event[i].icon.equals("health")) {
                drawable = R.drawable.red_cross;
            }
            if (event[i].icon.equals("leisure")) {
                drawable = R.drawable.tree;
            }
            if (event[i].icon.equals("other")) {
                drawable = R.drawable.star;
            }
            RowItem item = new RowItem(drawable, event[i].event, event[i].time);
            rowItems.add(item);
            listView = (ListView) calendarController.getViewPuppeteer().getmActivity().findViewById(R.id.list);
            listenClickEvent(listView);
            ListAdapter adapter = new ListAdapter(this, rowItems);
            listView.setAdapter(adapter);
        }
    }

    /**
     * \brief Listen on the event to be deleting
     * \param listView  The list view of event
     */
    public void listenClickEvent(final ListView listView){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RowItem event = (RowItem)listView.getItemAtPosition(position);
                calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().deleteEvent(event.getTitle());
                positionToDelete = position;
            }
        });
    }

    /**
     * \brief Give the size of the list
     * \return The number of item
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * \brief Give the position of the item
     * \param position  The position of the item
     * \return position The data at the specific position
     */
    @Override
    public Object getItem(int position) {
        return position;
    }

    /**
     * \brief Give the row of the item
     * \param position  The position of the item
     * \return position The id of the item at the specific position
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * \brief Inflate the view with the grids cell
     * \param position  The position of the item
     * \param convertView   The new view
     * \param parent    The parent view
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) calendarController.getViewPuppeteer().getmActivity().getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.calendar_gridcell, parent, false);
        }
        String[] day_color = list.get(position).split("-");

        calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().setDay((TextView) row.findViewById(R.id.day));
        calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().setEvent_health((ImageView) row.findViewById(R.id.event_red_cross));
        calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().setEvent_leisure((ImageView) row.findViewById(R.id.event_tree));
        calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().setEvent_other((ImageView) row.findViewById(R.id.event_star));

        calendarController.getViewPuppeteer().getmActivity().getCalendarScreen().setDayButton((Button) row.findViewById(R.id.calendar_day_gridcell));

        updateView(day_color);
        return row;
    }
    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the calendarController class
     */
    public CalendarController getCalendarController() {
        return calendarController;
    }

    /**
     * \brief Get method for the dat of event
     */
    public String getDateOfEvent() {
        return dateOfEvent;
    }

    /**
     * \brief Get method for the event to delete
     */
    public int getPosition() {
        return positionToDelete;
    }
}
